import java.util.Stack;

public class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    public void minStack() {
        stack = new Stack<>();
        minStack = new Stack<>();

    }

    public void push(int val) {
        stack.push(val);//给stack压入一个值
        //最小栈为空
        if (minStack.empty()) {
            minStack.push(val);//压入到最小栈
        }else {
            //比较大小
            int stackTop = stack.peek();//看一下栈顶元素的值
            if (val <= stackTop) {
                //将这个栈顶元素压入到minStack中
                minStack.push(val);
            }
        }
    }

    public void pop() {
        if (!stack.empty()) {
            int stackX = stack.pop();//压入到stackX中
            //如果stack的栈顶的值等于最小栈的栈顶的值
            if (stackX == minStack.peek()) {
                //弹出
                minStack.pop();
            }
        }
    }

    public int top() {
        //不为空
        if (!stack.empty()) {
            //返回栈顶位置的下标
            return stack.peek();
        }
        return -1;
    }

    public int getMin() {
        if (minStack.empty()) {
            return -1;
        }
        return minStack.peek();//不为空返回栈顶的下标 - 栈顶会一直方最小值
    }
}
